#include "logger.h"

Logger* Logger::_pInstance = nullptr;
Category* Logger:: _root = nullptr;

Logger::Logger(){
    //输出到标准输入
    OstreamAppender *ap1 = new OstreamAppender("console", &cout);
    PatternLayout *p1 = new PatternLayout();
    p1->setConversionPattern("%d %c [%p] %m%n");
    ap1->setLayout(p1);
    //输出到文件
    FileAppender *ap2 = new FileAppender("file","singleton.log");
    PatternLayout *p2 = new PatternLayout();
    p2->setConversionPattern("%d %c [%p] %m%n");
    ap2->setLayout(p2);

    _root = & Category::getRoot();
    _root->setPriority(Priority::INFO);
    _root->setAppender(ap1);
    _root->setAppender(ap2);

}

Logger::~Logger(){
    Category::shutdown();
}

void Logger::destroy(){
    if(_pInstance){
        delete _pInstance;
        _pInstance = nullptr;
    }
}
    
Logger* Logger::getInstance(){
    if(_pInstance == nullptr){
        _pInstance = new Logger();
    }
    return _pInstance;
}


void Logger::warn(const char * file, int line, const char * func, const char * msg){
    string ch_space = " ";
    string temp = file + ch_space +to_string(line)+ " " + func +" "+ msg;
    _root->warn(temp);
}
void Logger::error(const char * file, int line, const char * func, const char * msg){
    string ch_space = " ";
    string temp = file + ch_space +to_string(line)+ " " + func +" "+ msg;
    _root->error(temp);
}
void Logger::debug(const char * file, int line, const char * func, const char * msg){
    string ch_space = " ";
    string temp = file + ch_space +to_string(line)+ " " + func +" "+ msg;
    _root->debug(temp);
}
void Logger::info(const char * file, int line, const char * func, const char * msg){
    string ch_space = " ";
    string temp = file + ch_space +to_string(line)+ " " + func +" "+ msg;
    _root->info(temp);
 
}
